package com.somnus.androidkotlindemo.databinding

import android.content.Context
import android.content.Intent
import android.databinding.ObservableField
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.bean.User
import kotlinx.android.synthetic.main.content_data_bing_list.*
import kotlinx.android.synthetic.main.recycler_view.*

class DataBingListActivity : AppCompatActivity() {

    //    var dataAdapter = DataBindingAdapter()
    lateinit var dataAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recycler_view)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        dataAdapter = MyAdapter()
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = dataAdapter
        dataAdapter.setNewData(getList())
        dataAdapter.setOnItemClickListener { _adapter, _view, _position ->
            val _User = _adapter.getItem(_position) as User


//            val mUser = User()
//            mUser.userName.set("德玛西亚")
//            mUser.userNickName.set("德玛西亚丫丫")

//             dataAdapter.data[_position].userName.set("德玛西亚")
//             dataAdapter.data[_position].userNickName.set("德玛西亚丫丫")

            _User.userName.set("点击改变Name")
            _User.userNickName.set("点击改变NickName")
//            dataAdapter.notifyItemChanged(_position) //不需要在去用该方法更新adapter

        }

    }

    fun getList(): MutableList<User> {
        val mlist = mutableListOf<User>()
        for (item in 0..10) {
            val user = User()
            user.userName = ObservableField("账号$item")
            user.userNickName = ObservableField("昵称账号$item")
            mlist.add(user)
        }
        return mlist
    }

    //创建一个 伴生对象
    companion object {
        fun startDataBingListActivity(context: Context) {

            val mIntent = Intent()
            mIntent.setClass(context, DataBingListActivity::class.java)
            context.startActivity(mIntent)
        }
    }


}
